package com.fick.tools;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringConverter {

	public static String decodeString(String utf8) {
		if (utf8 == null) {
			return "";
		}
		String gbk = "";
		try {
			gbk = URLDecoder.decode(utf8, "ISO-8859-1");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return gbk;
	}

	public static String encodeString(String gbk) {
		if (gbk == null) {
			return "";
		}
		String utf8 = "";
		try {
			utf8 = URLEncoder.encode(gbk, "UTF8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}

		return utf8;
	}

	public static String getRedString(String str, String red) {
		String redHtml = "<font color=\"red\">" + red + "</font>";
		str = str.replace(red, redHtml);
		return str;
	}

	/***************************************************************************
	 * Method public static String getString(String html); ȥ���ַ��е�html����
	 * 
	 * @param String
	 *          html ��Ҫȥ��html������ַ�
	 * @return String str ȥ��html������ַ�
	 */
	public static String getString(String html) {
		String regEx = "<.+?>"; // ��ʾ��ǩ
		Pattern p = Pattern.compile(regEx);
		Matcher m = p.matcher(html);
		String str = m.replaceAll("");
		return str;
	}

	/***************************************************************************
	 * Method public static String subString(String str, int length, boolean
	 * addDot); �ض��ַ�
	 * 
	 * @param String
	 *          str ��Ҫ��ȡ���ַ�
	 * @param int length ��ȡ�ĳ���
	 * @param boolean addDot ��ȡ���Ƿ���ĩβ����...
	 * @return String str ���ؽ�ȡ����ַ�
	 */
	public static String subString(String str, int length, boolean addDot) {
		if (str.length() < length) {
			return str;
		} else {
			str = str.substring(0, length);
			if (addDot) {
				str += "...";
			}
			return str;
		}
	}

}
